from PyQt4.QtGui import QGraphicsObject,QPen,QBrush,QColor
from PyQt4 import QtCore,QtGui,Qt
from KCharts.KGraphics.KGraphicsSceneSubViewModel import KGraphicsSceneSubViewModel
from KCharts.KGraphics.KBaseDisplayItem import KBaseDisplayItem,KBaseDisplayItemModel
from KApp import KConstants as vm

class KPriceItemModel(KBaseDisplayItemModel):
	
	def GetLogicRange(self):
		(xdatarange,ydatarange)=self.m_stock.CurWindowRange('Price')	
		return (xdatarange,ydatarange)
	


class KPriceDisplayItem(KBaseDisplayItem):	
	def PaintPrice(self,painter):
		
		painter.setPen(QPen(QColor(255,255,255)))
		priceblocks=self.m_stock.CurWindowData('Price')
		XCordinates=self.m_stock.CurWindowData('Index')
		width=1.0/3.0
		#(test,())=self.viewmodel.mapLogiceSizeToScene((width,))
		[width],[]=self.viewmodel.mapLogiceSizeToScene(vm.RectType.ContentBound,[width])

		for index in range(0,len(priceblocks)):
			(openp,highp,lowp,closep,vol)=priceblocks[index]
			xcord=XCordinates[index]
			([xcord],[openp,highp,lowp,closep])=self.viewmodel.mapLogiceCordToScene(vm.RectType.ContentBound,[xcord],[openp,highp,lowp,closep])

			if abs(highp-lowp)>0.00001:	# draw high/low when not equal
				painter.drawLine(QtCore.QPointF(xcord, lowp), QtCore.QPointF(xcord, highp))

			if openp > closep:
				painter.setBrush(QBrush(QColor(0,255,0)))			
				
			else:
				painter.setBrush(QBrush(QColor(255,0,0)))	

			painter.drawRect(QtCore.QRectF(xcord-width,closep,width*2, openp-closep))
	
	def PrepareRenderPicture(self):
		## pre-computing a QPicture object allows paint() to run much more quickly, 
		## rather than re-drawing the shapes every time.

		
		self.picture = QtGui.QPicture()
		painter = QtGui.QPainter(self.picture)	
		self.PaintGrid(painter,vm.RectType.ContentBound,[0,5])
		self.PaintBorders(painter,vm.RectType.OuterBound)
		self.PaintBorders(painter,vm.RectType.InnerBound)
		self.PaintBorders(painter,vm.RectType.ContentBound)
		#self.PaintBorders(painter,vm.RectType.XAxisBottom)
		#self.PaintBorders(painter,vm.RectType.YAxisLeft)
		#self.PaintBorders(painter,vm.RectType.XAxisTop)
		#self.PaintBorders(painter,vm.RectType.YAxisRight)
		self.PaintPrice(painter)
		self.PaintXAxis(painter,vm.RectType.XAxisBottom,10)
		#self.PaintYAxis(painter,vm.RectType.YAxisLeft,5)
		#self.PaintXAxis(painter,vm.RectType.XAxisTop,10)
		self.PaintYAxis(painter,vm.RectType.YAxisRight,5)			
		painter.end()		
